// pages/ucenter/ucenter.js
var check = require('../../../utils/check.js');

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    channel: [],
    userInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("ucenter page onload get data");
    this.setData({
      permitChannel: [
        {
          id: 0,
          type: 'page',
          enable: true,
          iconUrl: "/static/patient/images/my_nurse.png",
          name: "我的护士",
          url: "/pages/patient/nurseinfo/nurseinfo"
        },
        {
          id: 1,
          type: 'page',
          enable: true,
          iconUrl: "/static/patient/images/my_mantain.png",
          name: "我的维护",
          url: "/pages/patient/condition-records/records?patientId=" + app.globalData.metaId
        },
        {
          id: 2,
          type: 'page',
          enable: true,
          iconUrl: "/static/patient/images/my_observe.png",
          name: "我的观察",
          url: "/pages/patient/condition/condition"
        },
        {
          id: 3,
          type: 'page',
          enable: true,
          iconUrl: "/static/patient/images/my_query.png",
          name: "咨询记录",
          url: "/pages/common/chat-window/chat-window?local=" + app.globalData.metaId + "&peer=" + app.globalData.myNurseId
        },
        {
          id: 4,
          type: 'page',
          enable: true,
          iconUrl: "/static/patient/images/my_info.png",
          name: "我的资料",
          url: '/pages/patient/patientinfo/patientinfo'
        }
      ],
      
      noPermitChannel: [
        {
          id: 0,
          type: 'bindtap',
          enable: true,
          iconUrl: "/static/patient/images/my_nurse.png",
          name: "我的护士",
          url: "/pages/patient/nurseinfo/nurseinfo"
        },
        {
          id: 1,
          type: 'bindtap',
          enable: true,
          iconUrl: "/static/patient/images/my_mantain.png",
          name: "我的维护",
          url: "/pages/patient/condition-records/records?patientId=" + app.globalData.metaId
        },
        {
          id: 2,
          type: 'bindtap',
          enable: true,
          iconUrl: "/static/patient/images/my_observe.png",
          name: "我的观察",
          url: "/pages/patient/condition/condition"
        },
        {
          id: 3,
          type: 'bindtap',
          enable: true,
          iconUrl: "/static/patient/images/my_query.png",
          name: "咨询记录",
          url: "/pages/common/chat-window/chat-window?local=" + app.globalData.metaId + "&peer=" + app.globalData.myNurseId
        },
        {
          id: 4,
          type: 'page',
          enable: true,
          iconUrl: "/static/patient/images/my_info.png",
          name: "我的资料",
          url: '/pages/patient/patientinfo/patientinfo'
        }
      ]      
    });
    this.getIndexData();
    this.getUserInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getIndexData: function () {
    let that = this;
    check.checkPermit().then(function(res){
      if(res){
        that.setData({
          channel: that.data.permitChannel
        });
      }else{
        that.setData({
          channel: that.data.noPermitChannel
        });
      }
    });
  },

  getUserInfo: function () {
    let that = this;
    that.setData({
      userInfo: {
        id: app.globalData.patientNum,
        name: app.globalData.myName,
        nickName: "ZhangSan",
        headPortrait: app.globalData.avatarUrl,
        sex: app.globalData.mySex,
        birthday: app.globalData.myBirthday
      }
    });
  },

  btClick: function(event){
    let that = this;
    let id = event.currentTarget.dataset.id;
    console.log("btClick: " + id);
    check.checkPermit().then(function(res){
      if(res){
        that.setData({
          channel: that.data.permitChannel
        });
      }else{
        wx.showModal({
          title: '未审核，请耐心等待...'
        });
      }
    });
  },
})